package br.com.aquinoevasconcelos.filtros;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import br.com.aquinoevasconcelos.user.UsuarioAdm;

/**
 * Servlet Filter implementation class FiltroAdministrador
 */
@WebFilter("/FiltroAdministrador")
public class FiltroAdministrador implements Filter {

	public FiltroAdministrador() {
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {

		HttpServletRequest httpRequest = (HttpServletRequest) request;
		HttpServletResponse httpResponse = (HttpServletResponse) response;

		HttpSession session = httpRequest.getSession(false);
		try {

			UsuarioAdm adm = (UsuarioAdm) session
					.getAttribute("sessaoAdministrador");

			if (adm != null) {

				chain.doFilter(request, response);
			} else {

		   httpResponse.sendRedirect("../login_admin.jsp");
			}
		} catch (Exception e) {
			
			httpResponse.sendRedirect("../login_admin.jsp");
		}
	}

	public void destroy() {
	}

	public void init(FilterConfig fConfig) throws ServletException {
	}

}
